package com.genesis.yunnanji.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.genesis.yunnanji.Constants;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.BaseBean;
import com.genesis.yunnanji.bean.MessageEventBean;
import com.genesis.yunnanji.bean.UserInfoBean;
import com.genesis.yunnanji.utils.NetUtils;
import com.genesis.yunnanji.view.AboutAppActivity;
import com.genesis.yunnanji.view.AllyActivity;
import com.genesis.yunnanji.view.DeliveryAddressActivity;
import com.genesis.yunnanji.view.EarningsActivity;
import com.genesis.yunnanji.view.GeneralizeActivity;
import com.genesis.yunnanji.view.LoginActivity;
import com.genesis.yunnanji.view.ReturnGoodsActivity;
import com.genesis.yunnanji.view.SettingActivity;
import com.genesis.yunnanji.view.UserCollectionActivity;
import com.genesis.yunnanji.view.UserFootprintActivity;
import com.genesis.yunnanji.view.UserOrderActivity;
import com.genesis.yunnanji.view.UserSettingActivity;
import com.genesis.yunnanji.widget.GlideCircleTransform;
import com.youth.xframe.widget.XToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.bining.footstone.utils.SharedUtils;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.RequestBody;

/**
 * 创建日期：2018/9/19 on 17:31
 * 作者:Kite
 * 描述:我的
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.actionbar_back)
    ImageView actionbarBack;

    @BindView(R.id.actionbar_title)
    TextView actionbarTitle;

    @BindView(R.id.iv_head)
    ImageView ivHead;

    @BindView(R.id.tv_nickname)
    TextView tvNickname;

    @BindView(R.id.tv_edit)
    TextView tvEdit;

    @BindView(R.id.tv_generalize_num)
    TextView tvGeneralizeNum;

    @BindView(R.id.tv_ally_num)
    TextView tvAllyNum;

    @BindView(R.id.tv_earnings_num)
    TextView tvEarningsNum;

    @BindView(R.id.bbi_obligation)
    BottomBarItem bbiObligation;

    @BindView(R.id.bbi_to_send_the_goods)
    BottomBarItem bbiToSendTheGoods;

    @BindView(R.id.bbi_wait_receiving)
    BottomBarItem bbiWaitReceiving;

    @BindView(R.id.bbi_to_be_evaluated)
    BottomBarItem bbiToBeEvaluated;

    @BindView(R.id.bbi_exchange)
    BottomBarItem bbiExchange;

    @BindView(R.id.bbl)
    BottomBarLayout bbl;

    Unbinder unbinder;

    /**
     * Fragment 实例
     */
    public static MineFragment newInstance(Object... pramars) {
        MineFragment fragment = new MineFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        actionbarTitle.setText("我");
        initListener();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessageInfo(MessageEventBean messageEvent) {
        if(messageEvent.isDefault()){
            getUserInfo();
        }else {
            tvNickname.setText("您还未登录哦");
            Glide.with(getActivity())
                    .load(R.drawable.comment_defaultphoto)
                    .into(ivHead);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (SharedUtils.getBoolean(Constants.USERISLOGIN, false)) {
            getUserInfo();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initListener() {
        bbl.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
//                BottomBarItem bottomItem = bbl.getBottomItem(currentPosition);
                if (currentPosition == 4) {
//                    advanceIfLogin(ReturnGoodsActivity.class);
                    advance(ReturnGoodsActivity.class);
                } else {
                    advance(UserOrderActivity.class);
//                    advanceIfLogin(UserOrderActivity.class);
                }
            }
        });
//        bbl.setUnread(0, 20);//设置第一个页签的未读数为20
//        bbl.setUnread(1, 99);//设置第二个页签的未读数
//        bbl.showNotify(2);//设置第三个页签显示提示的小红点
//        bbl.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字

    }

    @OnClick({R.id.actionbar_back, R.id.center_layout, R.id.tv_order, R.id.tv_collection, R.id.tv_address, R.id.tv_footprint,
            R.id.tv_service_phone, R.id.tv_about, R.id.tv_setting, R.id.ll_ally, R.id.ll_generalize, R.id.ll_earnings})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_back:
                break;
            case R.id.center_layout:
                advanceIfLogin(UserSettingActivity.class);
                break;
            case R.id.tv_order:
                advanceIfLogin(UserOrderActivity.class);
//                advance(UserOrderActivity.class);
                break;
            case R.id.tv_collection://我的收藏
                advanceIfLogin(UserCollectionActivity.class);
//                advance(UserCollectionActivity.class);
                break;
            case R.id.tv_address:
                advance(DeliveryAddressActivity.class, 0);
                break;
            case R.id.tv_footprint://我的足迹
                advanceIfLogin(UserFootprintActivity.class);
//                advance(UserFootprintActivity.class);
                break;
            case R.id.tv_service_phone:
                break;
            case R.id.tv_about:
                advance(AboutAppActivity.class);
                break;
            case R.id.tv_setting:
                advance(SettingActivity.class);
                break;
            case R.id.ll_ally://购物卡
                advance(AllyActivity.class);
                break;
            case R.id.ll_generalize://优惠卷
                advance(GeneralizeActivity.class);
                break;
            case R.id.ll_earnings://积分
                advance(EarningsActivity.class);
                break;
        }
    }

    private void advanceIfLogin(Class<?> cls, Object... params) {
        if (SharedUtils.getBoolean(Constants.USERISLOGIN, false)) {
            advance(cls, params);
        } else {
            advance(LoginActivity.class);
        }
    }

    private void getUserInfo() {
        JSONObject params = new JSONObject();
        try {
            params.put("__apptoken__", SharedUtils.getString(Constants.USER_TOKEN));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = NetUtils.initNet(params);
        EasyHttp.post(Constants.MEMBER_INDEX)
//                .baseUrl(Constants.MAIN_ADMIN)
                .requestBody(requestBody)
                .execute(new CallBackProxy<BaseBean<UserInfoBean>, UserInfoBean>(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        XToast.error(e.getMessage());
                    }

                    @Override
                    public void onSuccess(UserInfoBean bean) {
                        tvNickname.setText(bean.getMember().getNickname());
                        RequestOptions options = new RequestOptions()
                                .placeholder(R.drawable.comment_defaultphoto)
                                .error(R.drawable.comment_defaultphoto)
                                .fallback(R.drawable.comment_defaultphoto)
                                .transform(new GlideCircleTransform(getActivity()));
                        Glide.with(getActivity())
                                .load(Constants.PIC_ADMIN + bean.getMember().getPhoto())
                                .apply(options)
                                .into(ivHead);
                        tvAllyNum.setText(bean.getMember().getAccount());
                        tvGeneralizeNum.setText(bean.getMember().getConpon() + "");
                        tvEarningsNum.setText(bean.getMember().getJifen());
                        bbl.setUnread(0, bean.getOrder().get_$1());
                        bbl.setUnread(1, bean.getOrder().get_$2());
                        bbl.setUnread(2,bean.getOrder().get_$3());
                        bbl.setUnread(3, bean.getOrder().get_$4());
                        bbl.setUnread(3, bean.getOrder().get_$5());
                    }
                }) {
                });
    }

}
